//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#include "WeTexture3D.h"

namespace WackiEngine
{

	Texture3D::Texture3D()
	{
		Texture::init(GL_TEXTURE_3D);
	}

	Texture3D::Texture3D(
		GLint format,
		GLsizei width,
		GLsizei height,
		GLsizei depth,
		GLenum srcDataFormat,
		GLenum srcDataType,
		const GLvoid *srcData
		)
	{
		Texture::init(GL_TEXTURE_3D);
		image(0, format, width, height, depth, srcDataFormat, srcDataType, srcData);
	}


	void Texture3D::image(
		GLint level,
		GLint format,
		GLsizei width,
		GLsizei height,
		GLsizei depth,
		GLenum srcDataFormat,
		GLenum srcDataType,
		const GLvoid *srcData
		)
	{
		assert(_handle != GL_ZERO);

		if(format == GL_DEPTH_COMPONENT) {
			switch(srcDataFormat) {
			case GL_DEPTH_COMPONENT:
			case GL_DEPTH_COMPONENT16:
			case GL_DEPTH_COMPONENT24:
			case GL_DEPTH_COMPONENT32:
			case GL_DEPTH_COMPONENT32F:
				// format is ok
				break;
			default:
				srcDataFormat = GL_DEPTH_COMPONENT;
				break;
			}
		}
		glTexImage3D(_target, level, format, width, height, depth, 0, srcDataFormat, srcDataType, srcData);

		updateParameters();
	}


	void Texture3D::subImage(
		GLint level,
		GLint xOffset,
		GLint yOffset,
		GLint zOffset,
		GLsizei width,
		GLsizei height,
		GLsizei depth,
		GLenum srcDataFormat,
		GLenum srcDataType,
		const GLvoid *srcData
		)
	{
		assert(_handle != GL_ZERO);
		assert(xOffset >= 0 && xOffset <= _size[0]);
		assert(yOffset >= 0 && xOffset <= _size[1]);
		assert(zOffset >= 0 && zOffset <= _size[2]);
		assert((xOffset + width) <= _size[0]);
		assert((yOffset + height) <= _size[1]);
		assert((zOffset + height) <= _size[2]);

		glTexSubImage3D(_target, level, xOffset, yOffset, zOffset, width, height, depth, srcDataFormat, srcDataType, srcData);

	}

}